package com.adcc.loadsheet.monitor.interceptor;

import com.adcc.aoc.devLib.common.log.Log;
import com.adcc.loadsheet.monitor.entity.User;
import com.adcc.loadsheet.monitor.util.UserFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    private UserFactory userFactory;

    /**
     * 校验登录用户
     *
     * @param username
     * @param password
     * @param role
     * @return
     */
    private boolean verifyUser(String username, String password, int role) {
        boolean result = false;
        try {
            if (role == 0) {
                String adminUserName = userFactory.getAdminUserName();
                String adminPassword = userFactory.getAdminPassword();
                if (adminUserName.equals(username) && adminPassword.equals(password)) {
                    result = true;
                }
            } else if (role == 1) {
                String operatorUserName = userFactory.getOperatorUserName();
                String operatorPassword = userFactory.getOperatorPassword();
                if (operatorUserName.equals(username) && operatorPassword.equals(password)) {
                    result = true;
                }
            }
        } catch (Exception ex) {
            Log.error(LoginInterceptor.class, "verifyUser() error", ex);
        }
        return result;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        //这里的User是登陆时放入session的
        User user = (User) session.getAttribute("user");
        //session中有user且通过检验，则返回true，否则返回false
        if (user != null) {
            if (verifyUser(user.getUsername(), user.getPassword(), user.getRole())) {
                return true;
            }
        }

        String requestURI = request.getRequestURI();
        //判断是否刷新页面操作
        if ("/index".equals(requestURI)){
            response.sendRedirect("/");
            return false;
        }
        // [2024-7-8：杨家兴：修改bug,程序重启或客户端清除缓存，导致seession丢失，页面重新登录后布局异常]
        //重定向标识加载响应头中
        response.setHeader("MYREDIRECTLOGIN", "REDIRECT");
        //设置重定向的路径
        response.setHeader("CONTENTPATH", request.getContextPath() + "/");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

}
